// 度转度°分′秒″
export function ToDegrees(val) {
  if (typeof (val) == "undefined" || val == "") {
    return "";
  }
  val += ""
  var i = val.indexOf('.');
  var strDu = i < 0 ? val : val.substring(0, i); // 获取度
  var strFen = 0;
  var strMiao = 0;
  if (i > 0) {
    strFen = "0" + val.substring(i);
    strFen = strFen * 60 + "";
    i = strFen.indexOf('.');
    if (i > 0) {
      strMiao = "0" + strFen.substring(i);
      strFen = strFen.substring(0, i); // 获取分
      strMiao = strMiao * 60 + "";
      i = strMiao.indexOf('.');
      strMiao = strMiao.substring(0, i + 4); // 取到小数点后面三位
      strMiao = parseFloat(strMiao).toFixed(2); // 精确小数点后面两位
    }
  }
  return strDu + "," + strFen + "," + strMiao;
}

// 只转到分
export function ToDegreesFen(val) {
  if (typeof (val) == "undefined" || val == "") {
    return "";
  }
  val += ""
  var i = val.indexOf('.');
  var strDu = i < 0 ? val : val.substring(0, i); // 获取度
  var strFen = 0;
  if (i > 0) {
    strFen = "0" + val.substring(i);
    strFen = strFen * 60 + "";
    strFen = parseFloat(strFen).toFixed(3); // 精确小数点后面两位
    if (strFen < 10) {
      strFen = "0" + strFen
    }
  } else {
    strFen = "00.000"
  }
  return strDu + "," + strFen
}

// 度°分′秒″转度
export function ToDigital(strDu, strFen, strMiao, len) {
  len = (len > 6 || typeof (len) == "undefined") ? 6 : len; // 精确到小数点后最多六位
  strDu = (typeof (strDu) == "undefined" || strDu == "") ? 0 : parseFloat(strDu);
  strFen = (typeof (strFen) == "undefined" || strFen == "") ? 0 : parseFloat(strFen) / 60;
  strMiao = (typeof (strMiao) == "undefined" || strMiao == "") ? 0 : parseFloat(strMiao) / 3600;
  var digital = strDu + strFen + strMiao;
  if (digital == 0) {
    return "";
  } else {
    return digital.toFixed(len);
  }
}

export function stringToAngle(row) {
  var list = row.split(",")
  let icon = ["°", "′", "″"]
  let string = ""
  for (let index in list) {
    string += list[index] + icon[index]
  }
  return string;
}

/**
 * 根据正负返回南北东西
 * @param {*} row
 * @param {*} type 经度（lon）纬度（lat）
 * @param {*} isToDeg 是否转化为分
 * @returns
 */
export function transfLonAndLat(row, type = 'lat', isToDeg = false) {
  if (row === null) {
    return ""
  }
  var obj = {
    lat: ["N", "S"],
    lon: ["E", "W"]
  }
  if (isToDeg) {
    row = stringToAngle(ToDegreesFen(row))
  }
  let i = 0
  if (row.indexOf("-") >= 0) {
    i = 1
    row = row.replace("-", "")
  }
  return row + obj[type][i]
}

/**
 * 根据台风中心风速计算台风风级
 * @param {*} target 中心最大风速
 * @param {*} arr
 * @param {*} start
 * @param {*} end
 * @returns
 */
var typhoonStrength = [0, 0.3, 1.6, 3.4, 5.5, 8.0, 10.8, 13.9, 17.2, 20.8, 24.5, 28.5, 32.7, 37.0, 41.5, 46.2, 51.0, 56.1, 61.2]
export function findStrength(target, arr = typhoonStrength, start, end) {
  start = start || 0;
  end = end || arr.length - 1;
  if (target > arr[end]) return end;
  if (target < arr[start]) return -1;
  if (end - 1 === start) return start;
  let mid = Math.floor((start + end) / 2);
  if (arr[mid] > target) {
    end = mid;
    return findStrength(target, arr, start, end);
  } else if (arr[mid - 1] < target) {
    start = mid;
    return findStrength(target, arr, start, end);
  } else {
    return mid;
  }
}

